/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 故我
 * Date: 2023-05-13
 * Time: 21:03
 */
public class Testdemo1 {
    public static void main(String[] args) {
        StringBuilder stringBuilder1 = new StringBuilder("hello");
        StringBuilder stringBuilder2 =  stringBuilder1;
        stringBuilder1.append(' ');//插入字符
        stringBuilder1.append("world");//插入字符串
        stringBuilder1.append(123);
        System.out.println(stringBuilder1);
        System.out.println(stringBuilder1 == stringBuilder2);
        System.out.println(stringBuilder1.charAt(8));
        System.out.println(stringBuilder1.length());
        System.out.println(stringBuilder1.capacity());
        stringBuilder1.setCharAt(0,'u');//替换
        stringBuilder1.insert(1," hsauihfusahfsa ");
        System.out.println(stringBuilder1);
        stringBuilder1.deleteCharAt(0);
        stringBuilder1.delete(0,3);

        String str1 = stringBuilder1.substring(0,6);
        System.out.println(str1);
        stringBuilder1.reverse();
        str1=stringBuilder1.toString();
        System.out.println(stringBuilder1);
        System.out.println(str1);
    }
    public static void main19(String[] args) {
        long start = System.currentTimeMillis();
        String s="";
        for(int i=0;i<100000;i++){
            s+=i;
        }
        long end = System.currentTimeMillis();
        System.out.println(end - start);

        start = System.currentTimeMillis();
        StringBuilder stringbuilder = new StringBuilder("");
        for(int i=0;i<100000;i++){
            stringbuilder.append(i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);

        start = System.currentTimeMillis();
        StringBuffer stringbuffer = new StringBuffer();//加锁解锁有消耗
        for(int i=0;i<100000;i++){
            stringbuilder.append(i);
        }
        end = System.currentTimeMillis();
        System.out.println(end - start);

    }
    public static void main18(String[] args) {
        String str="hello";//创建了一个StringBuilder对象,append了hello 与 world
        str+="world";
        System.out.println(str);
    }
    public static void main17(String[] args) {
        //大小写转化
        String str=" hello %$$$ shiafia% 哈哈哈哈 ";
        System.out.println(str.toUpperCase());//大
        System.out.println(str.toLowerCase());//小  只转化字符
    }
    public static void main16(String[] args) {
        // trim去掉两边的空白字符
        String s=" hello world ";
        System.out.println("["+s.trim()+"]");
        System.out.println("["+s+"]");
    }
    public static void main15(String[] args) {
        //字符串截取
        String s="helloworld";
        System.out.println(s.substring(5));//从索引位置开始截图
        System.out.println(s.substring(0,5));//[)  左闭右开
    }
    public static void main14(String[] args) {
        String s="name=zhangsan&age=18";
        String[] str = s.split("&");
        for(int i=0;i<str.length;i++){
            String[] str1=str[i].split("=");
            System.out.println(str1[0]+" <-> "+str1[1]);
        }
    }
    public static void main13(String[] args) {
        String s1="ahus fheua.suhf*fefa\\ehua";
        String[] s = s1.split(" |\\.|\\*|\\\\");
        for(String e:s){
            System.out.println(e);
        }
    }
    public static void main12(String[] args) {
        //转换IP地址
        String s="127.0.0.1";
        String[] res = s.split("\\.");
        for(String e:res){
            System.out.println(e);
        }
    }
    public static void main11(String[] args) {
        String s2="Hello world sjai iahjfe eihfe jefjei eihf";
        String[] res = s2.split(" ");
        for(String s:res){
            System.out.println(s+" ");
        }
        System.out.println();

        String[] ret = s2.split(" ",2);//指定分成几组
        for(String s:ret){
            System.out.println(s);
        }
        System.out.println();
    }
    public static void main10(String[] args) {
        String s1="hellofirst";
        System.out.println(s1.replaceAll("l","_"));
        System.out.println(s1.replaceFirst("f","t"));
    }
    public static void main9(String[] args) {
        //格式化
        String s=String.format("%d-%d-%d\n",2023,5,18);
        System.out.println(s);
    }
    public static void main8(String[] args) {
        //字符串转成数值
        String s="hahaeheihei";
        char[] ch = s.toCharArray();
        for(char c:ch){
            System.out.print(c+" ");
        }
        System.out.println();

        //数组转成字符串
        String s2=new String(ch);
        System.out.println(s2);
    }
    public static void main7(String[] args) {
        //大小写转换
        String s1="hello";
        String s2="HELOPLFJD";

        System.out.println(s1.toUpperCase());
        System.out.println(s2.toLowerCase());
    }
    public static void main6(String[] args) {
        //数值转化成字符串
        String s1 = String.valueOf(1234);
        String s2=String.valueOf(12.98);
        String s3=String.valueOf(true);
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s3);
        System.out.println("======================");
        //字符串转换为数值
        int a1= Integer.parseInt("1678");
        double b2 = Double.parseDouble("78.192");
        System.out.println(a1);
        System.out.println(b2);

    }
    public static void main5(String[] args) {
        String s1="acbcbcbfajsfsabcdewf";
        System.out.println(s1.charAt(4));
        System.out.println(s1.indexOf('b'));//返回字符第一次出现的位置
        System.out.println(s1.indexOf('b',7));
        System.out.println(s1.indexOf("abcde"));
        System.out.println(s1.indexOf("abc",7));
        System.out.println(s1.lastIndexOf("abc"));
        System.out.println(s1.lastIndexOf("acb",7));
        System.out.println(s1.lastIndexOf('c',7));
        System.out.println(s1.lastIndexOf('b'));
    }
    public static void main4(String[] args) {
        String s1=new String("abc");
        String s2=new String("abr");
        String s3=new String("abr");
        String s4=new String("abcsfdfegg");
        String s5 = new String("ABCSFDFEGG");
        System.out.println(s5.compareToIgnoreCase(s4));//忽略大小写

        System.out.println(s1.compareTo(s2));//返回ASCII的差值
        System.out.println(s2.compareTo(s3));
        System.out.println(s1.compareTo(s4));//返回长度差  前k个字符相同
    }

    private char[] value;
    private int hash;
    /*public boolean equals(Object object){
        if(this == object){
            return true;
        }
        //判断是否是子类
        if(object instanceof String){
            //向下转型
            String anobject = (String)object;
            int n = value.length;

            if(n == anobject.value.length){
                char[] c1= this.value;
                char[] c2=anobject.value;
                int index =0;
                while(index--!=0){
                    if(c1[index]!=c2[index]){
                        return false;
                    }
                    index++;
                }
            }
            return true;
        }
        return false;
    }*/
    public static void main3(String[] args) {
        String s1=new String("hello");
        String s2=new String("world");
        String s3=new String("hello");

        System.out.println(s1.equals(s3));
        System.out.println(s2.equals(s3));
    }
    public static void main2(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        String s3 = new String("world");
        String s4 = s1;
        System.out.println(s1 == s3);
        System.out.println(s1==s4);
    }

    public static void main1(String[] args) {
        String s1 = "hello world";//这个在常量池里面
        String s2 = new String("Hello 你好");
        char[] array = {'H', 'w', 'a', 'g'};
        String s3 = new String(array);//使用字符数组构造
        //System.out.println(s3);
        String s4 = s1;

        System.out.println(s1.length());
        System.out.println(s4.isEmpty());//判断长度是否为0

        System.out.println("hello".length());
    }
}
